package com.bwie.task;

import cn.hutool.core.bean.BeanUtil;
import com.bwie.es.EsProd;
import com.bwie.mapper.TbProdMapper;
import com.bwie.pojo.TbProd;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @description :
 * @Author QiHainan
 * @Create 2025/3/29 10:52
 * @Version 1.0
 */
@Component
@Slf4j //打印日志
public class SyncProdTask {
    @Autowired
    private TbProdMapper tbProdMapper;
    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

//    @Scheduled(cron = "0 0/1 * * * ?")
    public void syncProdToEs(){
        log.info("开始同步商品到Es");
        //从数据库中读取所有的商品数据
        List<TbProd> tbProds = tbProdMapper.selectList(null);
        //把商品信息存入Es中
        for (TbProd tbProd : tbProds) {
            EsProd esProd = BeanUtil.toBean(tbProd, EsProd.class);
            elasticsearchRestTemplate.save(esProd);
        }
        log.info("同步商品到Es完成");
    }

}
